package com.yanqu.road.server.cmd.activity.summergames;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Map;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_APPLY, desc = "申请交换请求")
@CmdApi(reqMsg = SummerGamesProto.SummerGamesApplyExchangeReqMsg.class, respMsg = SummerGamesProto.SummerGamesApplyExchangeRespMsg.class)
public class SummerGamesApplyExchangeCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (config == null || !ActivityMgr.activityInTimeAndShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        SummerGamesProto.SummerGamesApplyExchangeReqMsg reqMsg = SummerGamesProto.SummerGamesApplyExchangeReqMsg.parseFrom(packet.getBytes());
        GoodsInfo wantGoodsInfo = GoodsMgr.getGoodsById(reqMsg.getWantId());
        if(wantGoodsInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(reqMsg.getPayId());
        if(goodsInfo == null){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if(wantGoodsInfo.getGoodsId() == goodsInfo.getGoodsId()){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }

        if(wantGoodsInfo.getQuality() != goodsInfo.getQuality()){

            //品质不同
            ret = GameErrorCode.E_SUMMER_GAMES_EXCHANGE_QUALITY_DIFFERENT;
            return;
        }

        Property cost = PropertyHelper.parseStringToProperty(reqMsg.getPayId() + "=1");
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(cost)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }


        long count = player.getModule(BagModule.class).getUserBagItemCount(reqMsg.getPayId());
        if(count < config.getSUMMERGAME_EXCHANGE_NUM_LIMIT()){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }

        String unionUid = "";
        if(reqMsg.hasUnion() && reqMsg.getUnion()){
            if(StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())){
                ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
                return;
            }
            unionUid = player.getUserInfo().getUnionUid();
        }

        long appointUserId = 0;
        long appointUserServerId = 0;
        if(reqMsg.hasAppUserId() && reqMsg.getAppUserId() > 0){
            Map<Long, Long> map = UserMgr.getUserServerByUserAliasId(reqMsg.getAppUserId(), config.getActivityId());
            if(map.isEmpty()){
                ret = GameErrorCode.E_USER_NO_FOUND;
                return;
            }
            for (Map.Entry<Long, Long> entry : map.entrySet()) {
                if(entry.getKey() == player.getUserId()){
                    continue;
                }
                appointUserId = entry.getKey();
                appointUserServerId = entry.getValue();
                break;
            }
            if(appointUserId <= 0){
                ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
                return;
            }
        }

        player.getModule(PreReduceModule.class).preReduce(PropertyHelper.parseGoodsToString(reqMsg.getPayId(), BigInteger.ONE));
        SummerGamesProto.CrossSummerGamesApplyExchangeReqMsg.Builder crossReq = SummerGamesProto.CrossSummerGamesApplyExchangeReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setPayId(reqMsg.getPayId());
        crossReq.setWantId(reqMsg.getWantId());
        crossReq.setUnion(unionUid);
        crossReq.setAppUserId(appointUserId);
        crossReq.setAppUserServerId(appointUserServerId);
        crossReq.setUserData(PlayerPb.parsePlayerBaseInfoTemp(player.getUserInfo()));
        player.sendPacket(Protocol.C_SUMMER_GAMES_EXCHANGE_APPLY, crossReq);

    }

    @Override
    public void sendData(GamePlayer player) {

        if(ret != 0){
            player.sendPacket(Protocol.U_SUMMER_GAMES_EXCHANGE_APPLY, SummerGamesProto.SummerGamesApplyExchangeRespMsg.newBuilder().setRet(ret));
        }

    }
}
